package com.block.sms.service.send;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.block.common.constant.AppRspCodeConstant;
import com.block.common.service.LogFactory;
import com.block.common.util.LocalDateUtil;
import com.block.db.entity.SmsConfigInfo;
import com.block.db.entity.SmsTemplateInfo;
import com.block.sms.dto.*;
import com.block.sms.service.ISmsSendService;
import org.slf4j.Logger;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/*
提交地址：http://www.51miaoxin.com/sms/send
提交方法：POST
提交格式：application/x-www-form-urlencoded

账号消息属性：行业短信
连接协议：HTTP
发送账号：10046h
发送密码：hm9249yy7n
提交速度：无限制
发送速度：500/秒
接受客户IP：125.120.77.141
在线文档：http://docs.51miaoxin.com/
 */
@Service
public class MiaoXinSmsSendServiceImpl implements ISmsSendService {

    private static final Logger log = LogFactory.getCommLog();

    @Override
    public SmsResultDTO sendSms(SmsParamDTO smsParamDTO, SmsTemplateInfo smsTemplate, SmsConfigInfo smsConfigInfo) {

        SmsResultDTO resultDTO = new SmsResultDTO();
        JSONArray paramArr = smsParamDTO.getParamArray();
        //accout获取的是用户ID  username  账户  账号
        String account = smsConfigInfo.getAccountNo();
        //content是获取的短信模板内容
        String content = StrUtil.format(smsTemplate.getTemplateContent(), paramArr.stream().toArray());

        //获取时间戳并且转为str
        String ts = LocalDateUtil.getNowDateStr(LocalDateUtil.TOTAL_DATE_TIME_SIMPLE);// “20161220202020”
        //格式化rawString
        String rawString = StrUtil.format("account={}&ts={}&secret={}", account, ts, smsConfigInfo.getAccountSecret());
        //hutool工具封装了sha1加密可以直接调用
        String token = SecureUtil.sha1(rawString);
        //按照接口文档给的参数 先 生成 在传入
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("account", account);
        paramMap.put("token", token);
        paramMap.put("ts", ts);
        paramMap.put("mobiles", smsParamDTO.getMobie());
        paramMap.put("content", content);

        HttpRequest httpRequest = HttpUtil.createPost(smsConfigInfo.getSendUrl());
        httpRequest.form(paramMap);

        String bodyResult = httpRequest.execute().body();
        log.info("【秒信短信服务】发送结束【requesturl】{}【paramMap】{}【bodyResult】{}",
                smsConfigInfo.getSendUrl(), JSON.toJSONString(paramMap), bodyResult);

        JSONObject resultJson = JSON.parseObject(bodyResult);
        Integer code = resultJson.getInteger("code");
        String message = resultJson.getString("message");

        resultDTO.setResult(bodyResult);
        resultDTO.setCode(String.valueOf(code));
        resultDTO.setMessage(message);
        if (ObjectUtil.equals(Integer.valueOf("0"), code)) {
            resultDTO.setSuccess(true);
            return resultDTO;
        }

        resultDTO.setSuccess(false);
        return resultDTO;
    }

    @Override
    public SmsBalanceDTO queryBalanceInfo(SmsConfigInfo smsConfigInfo) {
        return null;
    }

    @Override
    public QueryResultDTO querySendResult(QueryParamDTO queryParamDTO, SmsConfigInfo smsConfigInfo) {
        QueryResultDTO queryResultDTO = new QueryResultDTO();
        queryResultDTO.setStatus("SUCCESS");

        return queryResultDTO;
    }
}


